jQuery(document).ready(function() {

	if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
        //alert("Browser is Safari");
		jQuery("#Province").click(function() {
		var province = jQuery("#s2id_Province .select2-chosen").text();
		if(province=="Metro Manila") 
		  jQuery("#areabrgy").show();
		else
		  jQuery("#areabrgy").hide();
		  jQuery("#s2id_Barangay").removeClass("error");
		});
	
		jQuery("select#Province").change(function () {
		var $Province = $('#Province').val();
		if ($.trim($Province).length == 0) {
			jQuery("#s2id_Province").addClass("error");	
		} else { jQuery("#s2id_Province").removeClass("error"); }
		});
		
		jQuery("select#Barangay").change(function () {
		var $Barangay = $('#Barangay').val();
		if ($.trim($Barangay).length == 0) {
			jQuery("#s2id_Barangay").addClass("error");	
		} else { jQuery("#s2id_Barangay").removeClass("error"); }
		});
		
		jQuery("select#City").change(function () {
		var $City = $('#City').val();
		if ($.trim($City).length == 0) {
			jQuery("#s2id_City").addClass("error");	
		} else { jQuery("#s2id_City").removeClass("error"); }
		});
		
		jQuery("select#Pcode").change(function() {
			var $Pcode = $('#Pcode').val();
			if ($.trim($Pcode).length == 0) {
				jQuery("#s2id_Pcode").addClass("error");	
			} else { jQuery("#s2id_Pcode").removeClass("error"); }
		});
	}
	
	var current_pos = getCookie("fb_current_pos");
	if (current_pos > 1) {
		//console.log(current_pos + "TRUE");
		if(current_pos=="2") {
		jQuery("div.wizard a:nth-of-type(1)").attr("class","current done");
		jQuery("div.wizard a:nth-of-type(2)").attr("class","current");
		jQuery("div.wizard a:nth-of-type(3), div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");
		}
		if(current_pos=="3") {
		jQuery("div.wizard a:nth-of-type(1), div.wizard a:nth-of-type(2)").attr("class","current done");
		jQuery("div.wizard a:nth-of-type(3)").attr("class","current");
		jQuery("div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");
		}
		if(current_pos=="4") {
		jQuery("div.wizard a:nth-of-type(1), div.wizard a:nth-of-type(2),div.wizard a:nth-of-type(3)").attr("class","current done");
		jQuery("div.wizard a:nth-of-type(4)").attr("class","current");
		jQuery("div.wizard a:nth-of-type(5)").removeAttr("href");
		}
		if(current_pos=="5") {
		jQuery("div.wizard a:nth-of-type(1),div.wizard a:nth-of-type(2),div.wizard a:nth-of-type(3),div.wizard a:nth-of-type(4)").attr("class","current done");
		jQuery("div.wizard a:nth-of-type(5)").attr("class","width100 current");
		
		  var mode = getCookie("fb_mode");
		  var promo = getCookie("fb_promo");
		  var commitmentBox = getCookie("fb_commitmentBox");
		  var mobtel = getCookie("fb_mobtel");
		  var username = getCookie("fb_username");
		  var eadd = getCookie("fb_eadd");
		  var bday = getCookie("fb_bday");
		  var address = getCookie("fb_address");
		  
          if(mode!="edit"){ 
			jQuery(".promoBox, .commitmentBox, .prepaidBox, .profileBox").addClass("active");
			jQuery(".pnone").hide();
			jQuery('#promo').html(promo);
			jQuery('#commitmentBox').text(commitmentBox);
			jQuery('#mobtel').text(mobtel);
			jQuery('#name').text(username);
			jQuery('#email').text(eadd);
			jQuery('#dob').text(bday);
			jQuery('#add').text(address);
			jQuery("#pname, #pemail, #pdob, #padd").show();
		  }
		}
	} else {
		//console.log(current_pos + "FALSE");
		jQuery("div.wizard a:nth-of-type(1), div.wizard a:nth-of-type(2), div.wizard a:nth-of-type(3), div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");
	} 
	
	jQuery("#Fname").change(function() {
		var $Fname = $('#Fname').val();
		if ($.trim($Fname ).length == 0) {
			jQuery("#Fname").addClass("error");	
		} else { jQuery("#Fname").removeClass("error"); }
	});
	
	jQuery("#Mname").change(function() {
		var $Mname = $('#Mname').val();
		if ($.trim($Mname ).length == 0) {
			jQuery("#Mname").addClass("error");	
		} else { jQuery("#Mname").removeClass("error"); }
	});
	
	jQuery("#Lname").change(function() {
		var $Lname = $('#Lname').val();
		if ($.trim($Lname ).length == 0) {
			jQuery("#Lname").addClass("error");	
		} else { jQuery("#Lname").removeClass("error"); }
	});
	
	jQuery("#Eadd").change(function() {
		var $Eadd = $('#Eadd').val();
		if ($.trim($Eadd ).length == 0) {
			jQuery("#Eadd").addClass("error");	
			validateEmail($Eadd);
		} else { jQuery("#Eadd").removeClass("error"); }
	});
	
	jQuery("#datepicker").change(function() {
		var $datepicker = $('#datepicker').val();
		if ($.trim($datepicker ).length == 0) {
			jQuery("#datepicker").addClass("error");	
		} else { jQuery("#datepicker").removeClass("error"); }
	});
	
	jQuery("#Address").change(function() {
		var $Address = $('#Address').val();
		if ($.trim($Address).length == 0) {
			jQuery("#Address").addClass("error");	
		} else { jQuery("#Address").removeClass("error"); }
	});

	jQuery("select#Pcode").change(function() {
		var $Pcode = $('#Pcode').val();
		if ($.trim($Pcode).length == 0) {
			jQuery("#Pcode").addClass("error");	
		} else { jQuery("#Pcode").removeClass("error"); }
	});
	
	$("#btn_next").click(function() {
		var $Fname = $('#Fname').val();
		if ($.trim($Fname).length == 0) {
			$("#Fname").addClass("error");	
		} else { $("#Fname").removeClass("error"); }

		var $Mname = $('#Mname').val();
		if ($.trim($Mname ).length == 0) {
			$("#Mname").addClass("error");	
		} else { $("#Mname").removeClass("error"); }

		var $Lname = $('#Lname').val();
		if ($.trim($Lname).length == 0) {
			$("#Lname").addClass("error");	
		} else { $("#Lname").removeClass("error"); }

		var $Eadd = $('#Eadd').val();
		if ($.trim($Eadd ).length == 0) {
			$("#Eadd").addClass("error");	
		} else { validateEmail($Eadd); }
		
		var $Bday = $('#datepicker').val();
		if ($.trim($Bday ).length == 0) {
			$("#datepicker").addClass("error");	
		} else { $("#datepicker").removeClass("error"); }

		var $Address = $('#Address').val();
		if ($.trim($Address).length == 0) {
			$("#Address").addClass("error");	
		} else { $("#Address").removeClass("error"); }

		var $Province = $('#Province').val();
		if ($.trim($Province).length == 0) {
			$("#Province").addClass("error");	
		} else { $("#Province").removeClass("error"); }
		
		var $City = $('#City').val();
		if ($.trim($City ).length == 0) {
			$("#City ").addClass("error");	
		} else { $("#City ").removeClass("error"); }

		var $Barangay = $('#Barangay').val();
		if (jQuery("#areabrgy").is(":visible")){
			if ($.trim($Barangay).length == 0) 
			jQuery("#Barangay").addClass("error");
		} else { jQuery("#Barangay").removeClass("error"); }
		
		var $Pcode = $('#Pcode').val();
		if ($.trim($Pcode).length == 0) {
			$("#Pcode").addClass("error");	
		} else { $("#Pcode").removeClass("error"); }
		
		if(jQuery("#datepicker, #Province, #City, #Barangay, #Pcode").hasClass('error')) {
			FB.Canvas.scrollTo(0,900);
		}	
		
		if(jQuery("#Fname, #Mname, #Lname, #Eadd").hasClass('error')) {
			//scrollFrameHeight($('#Nname').height());
			FB.Canvas.scrollTo(0,500);
		}		
		
		if(jQuery("#Fname").hasClass('error')) a = 1; else a = 0;
		if(jQuery("#Mname").hasClass('error')) b = 1; else b = 0;
		if(jQuery("#Lname").hasClass('error')) c = 1; else c = 0;
		if(jQuery("#Eadd").hasClass('error')) d = 1; else d = 0;
		if(jQuery("#datepicker").hasClass('error')) e = 1; else e = 0;
		if(jQuery("#Address").hasClass('error')) f = 1; else f = 0;
		if(jQuery("#s2id_City").hasClass('error')) g = 1; else g = 0;
		if(jQuery("#s2id_Barangay").hasClass('error')) h = 1; else h = 0;
		if(jQuery("#s2id_Pcode").hasClass('error')) i = 1; else i = 0;
		if(jQuery("#s2id_Province").hasClass('error')) j = 1; else j = 0;
		var province = jQuery("#s2id_Province .select2-chosen").text();
		if(province=="Metro Manila") 	
			var toterr = parseInt(a) + parseInt(b) + parseInt(c) + parseInt(d) + parseInt(e) + parseInt(f) + parseInt(g) + parseInt(h) + parseInt(i) + parseInt(j);
		else
			var toterr = parseInt(a) + parseInt(b) + parseInt(c) + parseInt(d) + parseInt(e) + parseInt(f) + parseInt(g) + parseInt(i) + parseInt(j);
			
		if(jQuery.isEmptyObject(jQuery("#Pcode").val())==false){
		
		if(toterr=='0' && jQuery("#Pcode").val()!=""){ 
		
		var username = jQuery("#Fname").val() + " " + jQuery("#Mname").val() + " " + jQuery("#Lname").val();
		var mi = jQuery("#Mname").val();
		var sumname = jQuery("#Fname").val() + " " + mi.substring(0, 1) + ". " + jQuery("#Lname").val();
		var nname = jQuery("#Nname").val();
		var othecont = jQuery("#Othercont").val();
		var eadd = jQuery("#Eadd").val();
		var bday = jQuery("#datepicker").val();
		if(province=="Metro Manila") 
			var address = jQuery("#Address").val() + ', ' + jQuery("#s2id_Barangay .select2-chosen").text() + ', ' + jQuery("#s2id_City .select2-chosen").text() + ', ' + jQuery("#s2id_Province .select2-chosen").text() + ', ' + jQuery("#s2id_Pcode .select2-chosen").text();
		else
			var address = jQuery("#Address").val() + ', ' + jQuery("#s2id_City .select2-chosen").text() + ', ' + jQuery("#s2id_Province .select2-chosen").text() + ', ' + jQuery("#s2id_Pcode .select2-chosen").text();
		//alert(username + "-" + eadd + "-" + bday + "-" + address);
		document.cookie="fb_username=" + username + "; path=/";
		document.cookie="fb_sumname=" + sumname + "; path=/";
		document.cookie="fb_nname=" + nname + "; path=/";
		document.cookie="fb_othercont=" + othecont + "; path=/";
		document.cookie="fb_eadd=" + eadd + "; path=/";
		document.cookie="fb_bday=" + bday + "; path=/";
		document.cookie="fb_area=" + jQuery("#s2id_Barangay .select2-chosen").text() + "; path=/";
		document.cookie="fb_town=" + jQuery("#s2id_City .select2-chosen").text() + "; path=/";
		document.cookie="fb_province=" + jQuery("#s2id_Province .select2-chosen").text() + "; path=/";
		document.cookie="fb_postalcode=" + jQuery("#s2id_Pcode .select2-chosen").text() + "; path=/";
		document.cookie="fb_address=" + address + "; path=/";
		document.cookie="fb_postalIds=" + jQuery("#Province").val() + "|" + jQuery("#City").val() + "|" + jQuery("#Barangay").val() + "|" + jQuery("#Pcode").val() + "; path=/";
	 
		jQuery("#userinfo").submit();
			document.cookie="fb_current_pos=" + "5" + "; path=/";
		}
		
		}
			
	});
	
	jQuery("#Province").click(function() {
		var province = jQuery("#s2id_Province .select2-chosen").text();
		if(province=="Metro Manila") 
		  jQuery("#areabrgy").show();
		else
		  jQuery("#areabrgy").hide();
		  jQuery("#Barangay").removeClass("error");
	});
	
	jQuery("#Fname").keypress(function(evt){
        evt = (evt) ? evt : event;
        var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
        ((evt.which) ? evt.which : 0));
        if (charCode > 32 && (charCode < 37 || charCode > 40) && (charCode < 65 || charCode > 90) && (charCode < 97 || charCode > 122)) {
           return false;
        }
           return true;
    });

	jQuery("#Mname").keypress(function(evt){
        evt = (evt) ? evt : event;
        var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
        ((evt.which) ? evt.which : 0));
        if (charCode > 32 && (charCode < 37 || charCode > 40) && (charCode < 65 || charCode > 90) && (charCode < 97 || charCode > 122)) {
           return false;
        }
           return true;
    });
	
	jQuery("#Lname").keypress(function(evt){
        evt = (evt) ? evt : event;
        var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
        ((evt.which) ? evt.which : 0));
        if (charCode > 32 && (charCode < 37 || charCode > 40) && (charCode < 65 || charCode > 90) && (charCode < 97 || charCode > 122)) {
           return false;
        }
           return true;
    });
	
	jQuery("#Nname").keypress(function(evt){
        evt = (evt) ? evt : event;
        var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
        ((evt.which) ? evt.which : 0));
        if (charCode > 32 && (charCode < 37 || charCode > 40) && (charCode < 65 || charCode > 90) && (charCode < 97 || charCode > 122)) {
           return false;
        }
           return true;
    });
	
	jQuery("#Othercont").keypress(function (e) {
     	   //if the letter is not digit then display error and don't type anything
     	   if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
              return false;
    	   }
   	});
	
	if(/userprofile/.test(self.location.href)){
	   var mode = getCookie("fb_mode");
	   var globestore = getCookie("fb_globestore");
	   var username = getCookie("fb_username");
	   var eadd = getCookie("fb_eadd");
	   var bday = getCookie("fb_bday");
	   var address = getCookie("fb_address");
       if(mode=="edit"){
	     jQuery(".pnone").hide();
	     jQuery(".profileBox").addClass("active");
		 jQuery('#name').text(username);
		 jQuery('#email').text(eadd);
		 jQuery('#dob').text(bday);
		 jQuery('#add').text(address);
		 jQuery("#pname").show();
		 jQuery("#pemail").show();
		 jQuery("#pdob").show();
		 jQuery("#padd").show();
	   	 jQuery(".storeBox").addClass("active");
		 jQuery('#globestore').text(globestore);
		 jQuery('#btn_next2').text("Save");
       }
    }
	
	
});

   function getCookie(fb_cname) {
	var name = fb_cname + "=";
	var ca = document.cookie.split(';');
	for(var i=0; i<ca.length; i++) 
	{
	var c = ca[i].replace(/\s/g, "");
	if (c.indexOf(name)==0) return c.substring(name.length,c.length);
	}
	return "";
   }
   
   function getCookie(fb_name) {
    var nameEQ = fb_name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
   }


// ------------------------------
// Validate email	
// DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
// ------------------------------	
function validateEmail(email) {

		var at="@";
		var dot=".";
		var lat=email.indexOf(at);
		var lstr=email.length;
		var ldot=email.indexOf(dot);
		if (email.indexOf(at)==-1){
		   //alert("Invalid E-mail address")
		   //return false
		   jQuery("#Eadd").addClass("error");
		   jQuery("#email-error").show();
			return "Invalid E-mail address";
		}
		 else{
		 jQuery("#Eadd").removeClass("error");
		 jQuery("#email-error").hide();
		 }
		 
		if (email.indexOf(at)==-1 || email.indexOf(at)==0 || email.indexOf(at)==lstr){
		   //alert("Invalid E-mail address")
		   //return false
		   jQuery("#Eadd").addClass("error");
		   jQuery("#email-error").show();
		   return "Invalid E-mail address";
		}
		 else{
		 jQuery("#Eadd").removeClass("error");
		 jQuery("#email-error").hide();
		 }
		
		if (email.indexOf(dot)==-1 || email.indexOf(dot)==0 || email.indexOf(dot)==lstr){
		    //alert("Invalid E-mail address")
		    //return false
			jQuery("#Eadd").addClass("error");
			jQuery("#email-error").show();
			return "Invalid E-mail address";
		}
		 else{
		 jQuery("#Eadd").removeClass("error");
		 jQuery("#email-error").hide();
		 }
		
		 if (email.indexOf(at,(lat+1))!=-1){
		    //alert("Invalid E-mail address")
		    //return false
			jQuery("#Eadd").addClass("error");
			jQuery("#email-error").show();
			return "Invalid E-mail address";
		 }
		 else{
		 jQuery("#Eadd").removeClass("error");
		 jQuery("#email-error").hide();
		 }
		 
		 if (email.substring(lat-1,lat)==dot || email.substring(lat+1,lat+2)==dot){
		    //alert("Invalid E-mail address")
		    //return false
			jQuery("#Eadd").addClass("error");
			jQuery("#email-error").show();
			return "Invalid E-mail address"; 
		 }
		 else{
		 jQuery("#Eadd").removeClass("error");
		 jQuery("#email-error").hide();
		 }
		 
		 if (email.indexOf(dot,(lat+2))==-1){
		    //alert("Invalid E-mail address")
		    //return false
			jQuery("#Eadd").addClass("error");
			jQuery("#email-error").show();
		    return "Invalid E-mail address";
		 }
		 else{
		 jQuery("#Eadd").removeClass("error");
		 jQuery("#email-error").hide();
		 }
		
		 if (email.indexOf(" ")!=-1){
		    //alert("Invalid E-mail address")
		    //return false
			jQuery("#Eadd").addClass("error");
			jQuery("#email-error").show();
		    return "Invalid E-mail address";
		 }
		 else{
		 jQuery("#Eadd").removeClass("error");
		 jQuery("#email-error").hide();
		 }
		 
 		 return true;
	
}
